from django.contrib.auth.models import User
from django.db import models
from utils.modelsMinxin import ModelsSetMixin,IsDeleteModelsMixin,DatatimeModelsMixin
# Create your models here.


# 标签模型
class Label(DatatimeModelsMixin):
    name = models.CharField(max_length=100, unique=True,verbose_name="标签名")

    class Meta:
        db_table = 'label'

    def __str__(self):
        return self.name

# 文章模型
class Article(ModelsSetMixin):
    STATUS_CHOICES = ((0,"未发布"),(1,"发布"))
    title = models.CharField(max_length=100, unique=True,verbose_name="标题")
    digest = models.CharField(max_length=300, verbose_name="摘要")
    content = models.TextField(verbose_name="文章内容")
    page_view = models.IntegerField(default=0,verbose_name="浏览量")
    priority = models.IntegerField(default=0,verbose_name="优先级")
    status = models.IntegerField(default=0,verbose_name="状态",choices=STATUS_CHOICES)
    label = models.ForeignKey(Label,on_delete=models.SET_NULL,null=True)
    user = models.ForeignKey(User,on_delete=models.CASCADE)
    class Meta:
        db_table = 'article'
        ordering = ['-priority',"-page_view","-create_time"]

    def __str__(self):
        return self.title

# 评论模型
class Comment(ModelsSetMixin):
    content = models.TextField(verbose_name="评论内容")
    article = models.ForeignKey(Article, on_delete=models.CASCADE)
    user = models.ForeignKey(User, on_delete=models.CASCADE)
    # 关联自身   comment_set
    parent_comment = models.ForeignKey("self", verbose_name="父级评论id", null=True, blank=True,
                                       on_delete=models.CASCADE, related_name="parent_comment_set")
    # 回复的那一条评论   comment_set  related_name="reply_comment_set"  自定义管理器名
    reply_comment = models.ForeignKey("self", verbose_name="回复评论", null=True, blank=True, on_delete=models.CASCADE,
                                      related_name="reply_comment_set")

    class Meta:
        ordering = ['-create_time']
        db_table = 'Comment'

    def __str__(self):
        return f"{self.user}:{self.content}"